<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'coupontype'=>3);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_coupon',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['coupontype']=array('1'=>'购物礼品券','2'=>'充值礼品券','3'=>'礼品券');
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y/m/d H:i:s',$v['createtime']);
		$getallnum = pdo_count('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'couponid'=>$v['id']));
		if($v['t']<1){
			$v['last']='不限制';
		}else{
			$v['last']=$v['t']-$getallnum;
		}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'礼品券列表'),
			array('op'=>'buy','title'=>'添加礼品券'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>默认礼品券发放后，客户需求兑换礼品，才能获取里面的礼品！
		','edit_op'=>'buy','list'=>array(
			array('name'=>'title','title'=>'礼品券名称','type'=>'string'),
			array('name'=>'coupontype','title'=>'礼品券类型','type'=>'radio','options'=>'coupontype'),
			//array('name'=>'logo','title'=>'优惠','type'=>'image'),								
			array('name'=>'last','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'会员中心显示','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='buy'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_coupon',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['ismerch']=intval($item['ismerch']);
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个礼品券';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/card.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'礼品券的使用有时间限制，请及时使用！';
	$item['limittimes']=json_decode($item['limittimes'],true);
	$item['sendtime']=json_decode($item['sendtime'],true);
	$item['reward']=json_decode($item['reward'],true);
	$member=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','id desc');
	foreach($member as &$v){
		$id=$v['id'];
		$return['member'][$id]=$v['title'];
	}
	$setmerchids=array();
	if($this->plugin_exists('jy_merch')){
		$item['canmerch']=1;
		$limitmerchids=str_array($item['limitmerchids']);
		foreach($limitmerchids as $mvid){
			$merch=pdo_get('jy_merch_user',array('id'=>$mvid,'uniacid'=>$_SESSION['uniacid']),array('merchname','cateid'));
			$setmerchids[]=array('merchid'=>$mvid,'title'=>$merch['merchname'],'cateid'=>$merch['cateid']);
		}
		$item['setmerchids']=$setmerchids;
	}
	$return['item']=$item;
	$return['member'][0]='默认等级';
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$return['timelimittype']=array('0'=>'无限制','1'=>'获得多少天内','2'=>'时间段内');
	$shopset=$this->plugin_setting('shopset');
	$catelevel=2;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'礼品券列表'),
			array('op'=>'buy','title'=>'添加礼品券'),
		),
		'desc'=>'礼品券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'礼品券名称'),
					array('type'=>'image','name'=>'image','title'=>'图片'),				
					array('type'=>'string','name'=>'t','title'=>'发放总数','help'=>'0为不限制'),
				)
			),
			array(
				'title'=>'礼品设置',
				'list'=>array(
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：下面设置的礼品将全部发放给客户。'),
					array(
					'type'=>'array',
					'name'=>'reward',
					'add_tip'=>'添加一个奖项',
						'list'=>array(
							array('type'=>'string','title'=>'礼品名称','name'=>'title'),
							array('type'=>'radio','title'=>'礼品类型','name'=>'type','help'=>'','options'=>'prizeType'),
							array('type'=>'string','title'=>'单个红包最小金额','name'=>'redmin','show'=>array('type'=>'7')),
							array('type'=>'string','title'=>'单个红包最大金额','name'=>'rexmax','show'=>array('type'=>'7')),
							array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
						),
							'details'=>array(
								array('type'=>'string','title'=>'礼品名称','name'=>'title'),
								array('type'=>'radio','title'=>'礼品类型','name'=>'type','help'=>'支付参数--虚拟币--开启更多币种奖励','options'=>'prizeType'),
								array('name'=>'goodsid','title'=>'绑定商品','type'=>'choose','help'=>'','action'=>'jy_weishop/goods.get_goods/','show'=>array('type'=>1)),
								array('name'=>'couponid','title'=>'赠送优惠券','type'=>'choose','help'=>'','action'=>'jy_weishop/coupon.get_coupon//isall=1','show'=>array('type'=>9)),
								array('type'=>'string','title'=>'单个红包最小金额','name'=>'redmin','show'=>array('type'=>'7')),
								array('type'=>'string','title'=>'单个红包最大金额','name'=>'rexmax','show'=>array('type'=>'7')),
								array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
								array('type'=>'string','title'=>'补贴金额','name'=>'bufee','show'=>array('type'=>array(8))),
								
							)
					),
			)),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'status','options'=>'status','title'=>'加入领券中心'),
					array('type'=>'switch','name'=>'issendtime','title'=>'限制发放时间'),
					array('type'=>'time_range','name'=>'sendtime','title'=>'发放时间','show'=>array('issendtime'=>1)),		
					array('type'=>'string','name'=>'getmax','title'=>'每人限领次数'),
					array('type'=>'string','name'=>'tagtitle','title'=>'标签文字','show'=>array('status'=>1)),
					array('type'=>'radio','name'=>'settagtitlecolor','options'=>'is','title'=>'是否自定义标签颜色','show'=>array('status'=>1)),
					array('type'=>'color','name'=>'titlecolor','title'=>'标签颜色','show'=>array('status'=>1)),
					array('type'=>'switch','name'=>'islimitlevel','title'=>'是否限制会员等级'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'member','title'=>'选择可领取会员等级','show'=>array('islimitlevel'=>1)),					
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'ismerch','options'=>'ismerch','title'=>'适用范围','show'=>array('canmerch'=>1)),
					array('type'=>'array','show'=>array('ismerch'=>1),'name'=>'setmerchids','title'=>'选择多商户','add_tip'=>'添加一个多商户',
						'list'=>array(
							array('type'=>'select','name'=>'cateid','options'=>'merchcates','title'=>'商户分类'),
							array('type'=>'select','name'=>'merchid','options'=>'merchs','option_title'=>'merchname','title'=>'商户名称'),
						),
						'details'=>array(
							array('type'=>'select2_line','name'=>'cateid','options'=>'merchcates','title'=>'商户分类',
							'name2'=>'merchid','options2'=>'merchs','title2'=>'请选择商户',
							),
						)
					),			
				)
			),
			array(
				'title'=>'推送',
				'list'=>array(
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称 [total] 礼品券张数'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称 [total] 礼品券张数'),	
				)
			),
			array(
				'title'=>'使用说明',
				'list'=>array(
				
					array('type'=>'radio','name'=>'isdescsys','options'=>'isdescsys','title'=>'是否使用统一说明','help'=>'统一说明在【基础设置】中设置，如果使用统一说明，则在礼品券说明前面显示统一说明'),
					array('type'=>'text','name'=>'desc','title'=>'使用说明','help'=>'','show'=>array('isdescsys'=>0)),					
				)
			),
	)
	);
	$return['prizeType']=$this->prizeType;
	//多商户
	if($this->plugin_exists('jy_merch')){
		$merchcates=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid'],'pcate'=>0),array('catename','id'),'id');
		$return['merchcates']=array();
		foreach($merchcates as $v){
			$return['merchcates'][$v['id']]=array('id'=>$v['id'],'title'=>$v['catename']);
		}
		foreach($return['merchcates'] as &$mv){
			$mv['merchs']=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'cateid'=>$mv['id']),array('merchname','id'),'id');
			foreach($mv['merchs'] as &$mmv){
				$mmv['title']=$mmv['merchname'];
				unset($mmv['merchname']);
			}
		}
		$return['merchs']=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('merchname','id'),'id');
	}
	$return['ismerch']=array('平台礼品券','多商户礼品券','共用礼品券');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('deduct','issendtime','sendtime','paixu','title','bgcolor','getmax','memberlevels','backtype','gettype','image','groupid','enough','timelimittype','tagtitle','settagtitlecolor','titlecolor','islimitlevel','timedays','limittimes','coupontype','discount','discountmax','backmoney','backcredit','backredpack','t','status','sendtitle','senddesc','sendimage','desc','isdescsys','limitgoodcatetype','limitgoodtype','limitusetype','limitgoodcateids','limitgoodids'),$postdata);
	if($data['t']==0){
		//$data['t']=-1;
	}
	$data['limittimes']=ijson_encode($data['limittimes']);
	$data['sendtime']=ijson_encode($data['sendtime']);
	//多商户
	$limitmerchids=';';
	$setmerchids=$postdata['setmerchids'];
	foreach($setmerchids as $sv){
		$merchid=$sv['merchid'];
		if(strpos($limitmerchids,';'.$merchid.';') === false){
			$limitmerchids .=$merchid.';';
		}
	}
	$data['limitmerchids']=$limitmerchids;
	$data['ismerch']=$postdata['ismerch'];
	$data['merchtype']=$postdata['merchtype'];
	$data['reward']=ijson_encode($postdata['reward']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_coupon',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['coupontype']=3;
		$data['last']=$data['t'];
		pdo_insert('jy_weishop_coupon',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>